package com.yiwei.hdb.constant;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.Toast;

import com.yiwei.hdb.R;
import com.yiwei.hdb.activity.Application;
import com.yiwei.hdb.event.OnHomeEventMsg;
import com.yiwei.hdb.model.InitAppModel;
import com.yiwei.hdb.model.LoginAndRegisterModel;
import com.yiwei.hdb.model.NewBeginApplyStepModel;
import com.yiwei.hdb.model.UserModel;
import com.yiwei.hdb.util.PreferenceUtils;
import com.yiwei.hdb.util.ToastUtil;
import com.yiwei.hdb.util.Util;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by xsooy-pc on 17-12-19.
 */

public class UserGlobal {
    public static class DealInfo {
        public String deal_id = "";
        public int type;
        public String pay_bankcard = "";
        public ArrayList<NewBeginApplyStepModel> datas;
    }

    public static DealInfo info = new DealInfo();

    public static void saveLoginInfo(LoginAndRegisterModel.Data model) {
        PreferenceUtils.putString(Application.getInstance(), "username", model.getUser_name());
        PreferenceUtils.putString(Application.getInstance(), "mobile", model.getMobile());
        PreferenceUtils.putString(Application.getInstance(), "money", model.getMoney());
        PreferenceUtils.putBoolean(Application.getInstance(), "is_logined", true);
        PreferenceUtils.putBoolean(Application.getInstance(), "has_user_name", model.isHas_user_name());
        PreferenceUtils.putString(Application.getInstance(), "operator", model.getOperator());
    }

    public static void saveInitInfo(InitAppModel model) {
        PreferenceUtils.putBoolean(Application.getInstance(), "has_user_name", model.isHas_user_name());
        PreferenceUtils.putString(Application.getInstance(), "username", model.getUser_name());
        PreferenceUtils.putBoolean(Application.getInstance(), "is_logined", model.is_logined());
        PreferenceUtils.putString(Application.getInstance(), "lastest_version", model.getVersion().getAndroid().getLastest_version());
        PreferenceUtils.putInt(Application.getInstance(), "forced_upgrade", model.getVersion().getAndroid().getForced_upgrade());
        PreferenceUtils.putString(Application.getInstance(), "upgrade_msg", model.getVersion().getAndroid().getUpgrade_msg());
        PreferenceUtils.putString(Application.getInstance(), "download_url", model.getVersion().getAndroid().getDownload_url());
        PreferenceUtils.putString(Application.getInstance(), Const.KEY.SERVICE_PHONE, model.getContact_info().getService_phone());
        PreferenceUtils.putString(Application.getInstance(), Const.KEY.SERVICE_WEIXIN, model.getContact_info().getService_weixin());
        PreferenceUtils.putString(Application.getInstance(), Const.KEY.SERVICE_TIME, model.getContact_info().getService_time());
    }

    public static void saveUserInfo(UserModel model) {
        String newMoney = "";
        String repayMoney = "";
        if (model.getRepay_money().contains("￥")) {
            repayMoney = model.getRepay_money().replace("￥", "");
        } else {
            repayMoney = model.getRepay_money();
        }


        if (model.getMoney().contains("￥"))
            newMoney = model.getMoney().replace("￥", "");
        PreferenceUtils.putString(Application.getInstance(), "mobile", model.getMobile());
        PreferenceUtils.putString(Application.getInstance(), "username", model.getUsername() + "");
        PreferenceUtils.putString(Application.getInstance(), "money", newMoney + "");
        PreferenceUtils.putString(Application.getInstance(), "real_name", model.getReal_name() + "");
        PreferenceUtils.putString(Application.getInstance(), "u_alipay", model.u_alipay);
        PreferenceUtils.putString(Application.getInstance(), "address_city", model.getProvinces_cities() + "");
        PreferenceUtils.putInt(Application.getInstance(), "imposeDay", Integer.parseInt(model.getImpose_day()));
        PreferenceUtils.putInt(Application.getInstance(), "remainingDay", Integer.parseInt(model.getRemaining_days()));
        PreferenceUtils.putString(Application.getInstance(), "repayMoney", repayMoney + "");
        PreferenceUtils.putString(Application.getInstance(), "idcardpassed", model.getIdcardpassed());
        PreferenceUtils.putString(Application.getInstance(), "emailpassed", model.getEmailpassed());
        PreferenceUtils.putString(Application.getInstance(), "email", model.getEmail());
        PreferenceUtils.putString(Application.getInstance(), "header", model.getHeader());
        PreferenceUtils.putString(Application.getInstance(), "set_passwd", model.set_passwd);
        PreferenceUtils.putInt(Application.getInstance(), "sex", model.sex);
        PreferenceUtils.putInt(Application.getInstance(), Const.KEY.LEVEL, model.level_id);
    }

    public static boolean hasUsername() {
        if (TextUtils.isEmpty(PreferenceUtils.getString(Application.getInstance(), "username", "")) ||
                PreferenceUtils.getString(Application.getInstance(), "username", "").equals(PreferenceUtils.getString(Application.getInstance(), "mobile", ""))) {
            return false;
        } else {
            return true;
        }
    }


    public static String getEmail() {
        return PreferenceUtils.getString(Application.getInstance(), "email", "");
    }

    public static boolean getHasRepayMoney() {
        if (PreferenceUtils.getString(Application.getInstance(), "repayMoney", "0").equals("0")) {
            return false;
        } else {
            return true;
        }

    }

    public static String getUsername() {
        return PreferenceUtils.getString(Application.getInstance(), "username", "");
    }

    public static void setIsSetPass() {
        PreferenceUtils.putString(Application.getInstance(), "set_passwd", "1");
    }

    public static boolean isSetPass() {
        if (PreferenceUtils.getString(Application.getInstance(), "set_passwd", "0").equals("0")) {
            return false;
        } else {
            return true;
        }
    }

    public static void setRealName(String name) {
        PreferenceUtils.putString(Application.getInstance(), "real_name", name);
    }

    public static void setUseType(String use_type) {
        PreferenceUtils.putString(Application.getInstance(), "use_type", use_type);
    }

    public static String getUseType() {
        return PreferenceUtils.getString(Application.getInstance(), "use_type", "");
    }


    public static boolean isShowDealDialog(String deal_id) {
        if (PreferenceUtils.getString(Application.getInstance(), "deal_id_dialog", "").equals(deal_id)) {
            return false;
        } else {
            PreferenceUtils.putString(Application.getInstance(), "deal_id_dialog", deal_id);
            return true;
        }
    }

    public static void setDealId(String deal_id) {
        if (TextUtils.isEmpty(deal_id)) {
            deal_id = "";
        }
        PreferenceUtils.putString(Application.getInstance(), "deal_id", deal_id);
    }

    public static String getAlipay() {
        return PreferenceUtils.getString(Application.getInstance(), "u_alipay", "");
    }

    public static String getDealId() {
        return PreferenceUtils.getString(Application.getInstance(), "deal_id", "");
    }

    public static String getPayBankcard() {
        return PreferenceUtils.getString(Application.getInstance(), "pay_bankcard", "");
    }

    public static void setPayBankcard(String payBankcard) {
        PreferenceUtils.putString(Application.getInstance(), "pay_bankcard", payBankcard);
    }

    public static String getRealName() {
        return PreferenceUtils.getString(Application.getInstance(), "real_name", "");
    }

    public static String getMobile() {
        return PreferenceUtils.getString(Application.getInstance(), "mobile", "");
    }

    public static int getSex() {
        return PreferenceUtils.getInt(Application.getInstance(), "sex", 0);
    }

    public static String getHeader() {
        return PreferenceUtils.getString(Application.getInstance(), "header", "");
    }

    public static void setFindXueXinUsername(String find_xuexin_username) {
        PreferenceUtils.putString(Application.getInstance(), "find_xuexin_username", find_xuexin_username);
    }

    public static String getFindXueXinUsername() {
        return PreferenceUtils.getString(Application.getInstance(), "find_xuexin_username", "");
    }

    public static void setIsOpenGuesture(boolean isOpenGuesture) {
        PreferenceUtils.putBoolean(Application.getInstance(), "is_open_guesture", isOpenGuesture);
    }

    public static void setIsOpenGuestureRoot(boolean isGestrueRoot) {
        PreferenceUtils.putBoolean(Application.getInstance(), "is_open_guesture_root", isGestrueRoot);
    }

    public static boolean getIsOpenGuestureRoot() {
        return PreferenceUtils.getBoolean(Application.getInstance(), "is_open_guesture_root", true);
    }

    public static boolean getIsOpenGuesture() {
        return PreferenceUtils.getBoolean(Application.getInstance(), "is_open_guesture", false);
    }

    public static boolean getIsIdcardPass() {
        if (PreferenceUtils.getString(Application.getInstance(), "idcardpassed", "0").equals("1")) {
            return true;
        } else {
            return false;
        }
    }

    public static String getUserName() {
        String username = PreferenceUtils.getString(Application.getInstance(), "username", "");
        String mobile = PreferenceUtils.getString(Application.getInstance(), "mobile", "");
        if (!TextUtils.isEmpty(username)) {
            if (Util.isNumeric(username) && username.length() == 11)
                return Util.replaceString(username, 5, 4, '*');
            else
                return username;
        } else {
            if (Util.isNumeric(mobile) && mobile.length() == 11)
                return Util.replaceString(mobile, 5, 4, '*');
            else return mobile;
        }
    }

    public static boolean checkLogin() {
        if (!PreferenceUtils.getBoolean(Application.getInstance(), "is_logined", false)) {

            return false;
        }
        return true;
    }


    public static void logout() {
        Util.clearUserInfo(Application.getInstance());
        Map<String, String> map = new HashMap<>();
        map.put("type", Const.SendType.LOGOUT);
        EventBus.getDefault().post(new OnHomeEventMsg(map));
    }

    public static void login() {
        Map<String, String> map = new HashMap<>();
        map.put("type", Const.SendType.LOGIN);
        EventBus.getDefault().post(new OnHomeEventMsg(map));
        saveGoLogoutTime();
    }


    public static void saveGoLogoutTime() {
        PreferenceUtils.putLong(Application.getInstance(), "go_logout_time", System.currentTimeMillis());
    }

    public static boolean isNeedLogout() {
        if (System.currentTimeMillis() - PreferenceUtils.getLong(Application.getInstance(), "go_logout_time", System.currentTimeMillis()) > wait_time) {
            return true;
        } else {
            PreferenceUtils.putLong(Application.getInstance(), "go_logout_time", System.currentTimeMillis());
            return false;
        }
    }


    public static long wait_time = 1000 * 60 * 120;
}
